//
//  ReadSmallScrollView.m
//  product_B
//
//  Created by lanou on 16/5/28.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ReadSmallScrollView.h"
#import "ReadMainTableViewCell.h"
#import "ReadMainModel.h"
#import "ReadQuestionModel.h"
#import "ReadserialModel.h"
@interface ReadSmallScrollView()<UITableViewDelegate,UITableViewDataSource>
@end
@implementation ReadSmallScrollView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(20, 20, self.frame.size.width - 40, self.frame.size.height - 40+150)];
        self.tableView.layer.shadowOpacity = 0.4;
        self.tableView.layer.shadowColor = [UIColor blackColor].CGColor;
        self.tableView.layer.shadowOffset = CGSizeMake(1, 1);
        self.tableView.layer.shadowRadius = 4;
        _tableView.clipsToBounds = false;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        self.tableView.layer.cornerRadius = 5;
        [_tableView registerNib:[UINib nibWithNibName:@"ReadMainTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
//        _tableView.rowHeight = UITableViewAutomaticDimension;
//        _tableView.estimatedRowHeight = 2;
        [self addSubview:_tableView];
        self.contentSize = CGSizeMake(frame.size.width, frame.size.height + 50);
        self.showsVerticalScrollIndicator = NO;
    }
    return self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ReadMainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSArray *array = @[@"短篇",@"连载",@"问答"];
    if (indexPath.row == 0) {
        ReadMainModel *model = _modelArray[0];
        
        cell.titleLabel.text = model.hp_title;
        NSDictionary *dic = model.author[0];
        cell.authorLabel.text = dic[@"user_name"];
        cell.contentLabel.text= model.guide_word;
    }else if (indexPath.row == 1){
        ReadserialModel *model = _modelArray[1];
        cell.titleLabel.text  = model.title;
        cell.authorLabel.text = model.author[@"user_name"];
        cell.contentLabel.text = model.excerpt;
    }else{
        ReadQuestionModel *model = _modelArray[2];
        cell.titleLabel.text = model.question_title;
        cell.authorLabel.text = model.answer_title;
        cell.contentLabel.text = model.answer_content;
    }
    [cell.button setTitle:array[indexPath.row] forState:UIControlStateNormal];
    cell.button.layer.borderColor = [UIColor blueColor].CGColor;
    cell.button.layer.cornerRadius = 5;
    cell.button.layer.borderWidth = 1;
    cell.button.alpha = 0.5;
    cell.layer.cornerRadius = 5;
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (_readDelegate && [_readDelegate respondsToSelector:@selector(clickScrollWithIndexX:andIndexY:)]) {
        [_readDelegate clickScrollWithIndexX:_index andIndexY:indexPath.row];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return (self.frame.size.height - 40+150)/3;
}
@end
